<template>
  <div class="app-container">
    <el-dialog title="新增" :close-on-click-modal="false" :visible.sync="show" :fullscreen="true" append-to-body>
      <div v-loading="loading">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
          label-width="68px">
          <div class="app-space-between">
            <div class="app-space-between-left">
              <el-form-item label="商品分类" prop="goodsTypeId">
                <div style="width: 200px; font-size:13px;height:32px;">
                  <treeselect v-model="queryParams.goodsTypeId"
                    :options="[{goodsTypeId:'',goodsTypeName:'根目录'},...dataTable]" :normalizer="normalizer"
                    :show-count="true" placeholder="选择上级菜单" />
                </div>
              </el-form-item>
              <el-form-item label="商品名称" prop="goodsName">
                <el-input v-model="queryParams.goodsName" placeholder="请输入商品名称" clearable @clear="handleQuery"
                  @keyup.enter.native="handleQuery" />
              </el-form-item>
            </div>
            <div class="app-space-between-right">
              <el-form-item>
                <el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
                <el-button size="mini" @click="resetQuery">重置</el-button>
              </el-form-item>
            </div>
          </div>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table :data="postList" @selection-change="handleSelectionChange">
          <!-- <el-table-column type="selection" width="55" align="center" /> -->
          <el-table-column label="商品名称" align="center" prop="goodsName" />
          <el-table-column label="分类名称" align="center" prop="goodsTypeName" />
          <el-table-column label="商品价格(元)" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sellingPrice" />
            </template>
          </el-table-column>
          <el-table-column label="规格单位" align="center" prop="standards" />
          <el-table-column label="当前库存量" align="center">
            <template slot-scope="scope">
              <el-input v-model.number="scope.row.inventory" />
            </template>
          </el-table-column>

          <el-table-column label="最高库存量" align="center">
            <template slot-scope="scope">
              <el-input v-model.number="scope.row.maxInventory" />
            </template>
          </el-table-column>

          <el-table-column label="商品状态" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <div>{{scope.row.isOnshow}}</div>
              <el-button size="mini" type="text" @click="handleEvaluate(scope.row)">查看评论</el-button>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="handleSeletor(scope.row)">选择</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </div>
      <div slot='footer'>
        <el-button @click="cancel" :disabled="btnLoading">取 消</el-button>
      </div>
      <com-evaluate1 ref="com-evaluate1"></com-evaluate1>
    </el-dialog>
  </div>
</template>

<script>
  import Treeselect from "@riophae/vue-treeselect"
  import "@riophae/vue-treeselect/dist/vue-treeselect.css"
  import {
    goodsList
  } from "@/api/shopping/content"

  import {
    storeGoodsAdd
  } from "@/api/store/content"
  import ComEvaluate1 from '@/views/shopping/content/components/Evaluate'
  export default {
    dicts: ['sys_normal_disable'],
    components: {
      Treeselect,
      ComEvaluate1
    },
    props: ['dataOptions', 'dataTable'],
    data() {
      return {
        // 遮罩层
        loading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 岗位表格数据
        postList: [],
        // 弹出层标题
        title: "",
        btnLoading: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          goodsName: undefined,
          goodsTypeId: undefined
        },
        // 表单参数
        form: {},
        show: false,
        // 表单校验
        rules: {
          postName: [{
            required: true,
            message: "岗位名称不能为空",
            trigger: "blur"
          }],
          postCode: [{
            required: true,
            message: "岗位编码不能为空",
            trigger: "blur"
          }],
          postSort: [{
            required: true,
            message: "岗位顺序不能为空",
            trigger: "blur"
          }]
        }
      };
    },
    watch: {
      'queryParams.goodsTypeId'() {
        this.getList()
      }
    },
    created() {
      this.getList();
    },
    methods: {
      handleEvaluate(options){
        this.$refs['com-evaluate1'].init(options)
      },
      handleSeletor(options) {
        this.btnLoading = true
        this.loading = true
        storeGoodsAdd({
          storeId: this.$route.params.id,
          goodsId: options.goodsId,
          goodsName: options.goodsName,
          sellingPrice: options.sellingPrice,
          standards: options.standards,
          maxInventory: options.maxInventory,
          inventory: options.inventory
        }).then(res => {
          this.btnLoading = false
          this.loading = false
          this.show = false
          this.$message.success('操作成功')
          this.$emit('success')
        })
      },
      cancel() {
        this.show = false
      },
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.goodsTypeId,
          label: node.goodsTypeName,
          children: node.children
        };
      },
      init(str) {
        if (str == 'add') {
          this.show = true
          this.resetQuery()
        }
      },
      /** 查询岗位列表 */
      getList() {
        if (this.loading) return
        this.loading = true;
        let form = {
          goodsTypeName: '',
          ...this.queryParams
        }
        if (form.goodsTypeId) {
          this.dataOptions.forEach(item => {
            if (item.goodsTypeId == this.queryParams.goodsTypeId) form.goodsTypeName = item.goodsTypeName
          })
        }
        delete form.goodsTypeId

        goodsList(form).then(response => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 表单重置
      reset() {
        this.form = {
          goodsName: undefined,
          goodsTypeId: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.postId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 提交按钮 */
      submitForm() {},
    }
  };
</script>
